package com.ruoyi.web.core.task;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.biz.domain.*;
import com.ruoyi.biz.mapper.EquipmentInfoMapper;
import com.ruoyi.biz.mapper.SensorInfoMapper;
import com.ruoyi.biz.mapper.WarnedNoticeRecordMapper;
import com.ruoyi.biz.mapper.WarnedRecordMapper;
import com.ruoyi.biz.service.IChipInfoService;
import com.ruoyi.biz.service.IMaintainManageService;
import com.ruoyi.biz.service.ISensorInfoService;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.data.tdengine.domain.Sensor;
import com.ruoyi.data.tdengine.service.SensorService;
import com.ruoyi.system.service.*;
import com.ruoyi.web.core.utils.SendMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by james-YU on 2020/12/11
 * DESC:
 */
@Slf4j
@Component("mytask")
public class MyTask {

    @Autowired
    private IMaintainManageService maintainManageService;

    @Resource
    private SensorInfoMapper sensorInfoMapper;

    @Resource
    private ISensorInfoService sensorInfoService;
    @Resource
    private WarnedRecordMapper warnedRecordMapper;

    @Resource
    private SensorService sensorService;

    @Resource
    private WarnedNoticeRecordMapper warnedNoticeRecordMapper;

    @Resource
    private ISysConfigService sysConfigService;

    @Resource
    private EquipmentInfoMapper equipmentInfoMapper;
    @Autowired
    private IChipInfoService chipInfoService;
    @Resource
    private ISysUserService iSysUserService;
    /**
     * 查看保养是否过期
     */
    public void watchMaintainPlanEndTime() {
        List<MaintainManage> maintainManages = maintainManageService.selectAll().stream().filter(item -> item.getPlanEndTime().before(new Date())).collect(Collectors.toList());;
        log.debug("过期的保养对象:{}",maintainManages);
        maintainManages.forEach(item->{
            if (item.getStatus()!=44){
                item.setStatus(44);
                maintainManageService.updateMaintainManage(item);
            }
        });
    }


    /**
     * 定时任务每天零点清除通知次数
     */
    public void cleanNoticeNum(){
        List<SensorInfo> sensorInfos = sensorInfoMapper.selectSensorInfoList(new SensorInfo());
        sensorInfos.forEach(item->{
            item.setNoticeNum(0);//清零
            item.setNoticeDisable(0);//可发送状态
            //item.setAlarmType(0); //告警状态正常
            item.setNoticeTime(null);//清空发送时间
            sensorInfoMapper.updateSensorInfo(item);
        });
    }




    /**
     * 定时任务
     * 1.收集有问题的数据
     * 2.新增到报警记录 类型判断为预警/报警
     * 3.查看通知状态是否禁用，未禁用的发送通知
     * 4.发送通知成功的，将传感器的通知次数+1
     * 统一规范 0：离线 1，正常2.告警 3.预警
     */
    public void ryParams(){
    }

}
